package us.yunhe.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import us.yunhe.model.User;
import us.yunhe.server.UserServer;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
       
    public LoginServlet() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("username");
		String pwd = request.getParameter("password");

		UserServer userServer = new UserServer();
		User user = userServer.login(uname, pwd);
		if(user!=null){
			request.getSession().setAttribute("user", user);
			request.getRequestDispatcher("Main?pageIndex=1").forward(request, response);
		}else{
			response.sendRedirect("login.jsp");
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
